package com.ita.osc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.osc.bean.Dish;
import com.ita.osc.bean.User;
import com.ita.osc.service.DishManager;
import com.ita.osc.serviceImpl.DishManagerImpl;
import com.ita.osc.util.JsonUtil;

/**
 * Servlet implementation class DishListServlet
 */
public class DishListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	DishManager dishManager = new DishManagerImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DishListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		ServletContext context = request.getServletContext();
		PrintWriter writer = response.getWriter();
		List<Dish> list = null;
		if(context.getAttribute("dishList") != null){
			list = (List<Dish>) context.getAttribute("dishList");
		}else {
			list = dishManager.listAllDish();
			context.setAttribute("dishList", list);
		}
		writer.println(JsonUtil.objetc2Json(list));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
